<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="onumber">
        <el-input
          v-model="queryParams.onumber"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderaList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="订单" align="center" prop="oid" />-->
      <el-table-column label="订单编号" align="center" prop="onumber" />
      <el-table-column label="商品名称" align="center" prop="name" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="GroupNameRouter(scope.row)" >{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="买家昵称" align="center" prop="userName" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="UserB(scope.row)" >{{scope.row.userName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="购买份数" align="center" prop="itemCount" />
      <el-table-column label="金额" align="center" prop="price" />
      <el-table-column label="商家" align="center" prop="storeName1" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="store(scope.row)" >{{scope.row.storeName1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="提货人姓名" align="center" prop="reservedName" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" >
<!--        订单状态 0正常/1失败 -->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderStatus==0" type="success">正常</el-tag>
          <el-tag v-if="scope.row.orderStatus==1" type="danger">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="提货人电话" align="center" prop="reservedPhone" />
      <el-table-column label="提货状态" align="center" prop="itemStatus" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.itemStatus==0" type="success">已提货</el-tag>
          <el-tag v-if="scope.row.itemStatus==1" type="danger">未提货</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:ordera:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:ordera:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->

    </el-table>

    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        @size-change="handleSizeChangea"
        @current-change="handleCurrentChangea"
      />
    </div>


    <!-- 添加或修改订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="onumber">
          <el-input disabled v-model="form.onumber" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item  label="商品名称" prop="onumber">
          <el-input disabled v-model="form.name1" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="卖家昵称" prop="onumber">
          <el-input v-model="form.reservedName" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="商家" prop="onumber">
          <el-input v-model="form.storeName1" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="金额" prop="price">
          <el-input disabled v-model="form.price" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="购买份数" prop="itemCount">
          <el-input v-model="form.itemCount" placeholder="请输入购买份数" />
        </el-form-item>
        <el-form-item label="提货人姓名" prop="itemCount">
          <el-input v-model="form.reservedName" placeholder="请输入购买份数" />
        </el-form-item>
        <el-form-item label="订单状态" prop="itemCount">
          <el-input v-model="form.orderStatus" placeholder="请输入购买份数" />
        </el-form-item>
        <el-form-item label="提货人电话" prop="itemCount">
          <el-input v-model="form.reservedPhone" placeholder="请输入购买份数" />
        </el-form-item>
        <el-form-item label="提货状态" prop="itemCount">
          <el-input v-model="form.itemStatus" placeholder="请输入购买份数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrdera, getOrdera, delOrdera, addOrdera, updateOrdera, itemOrder0, orderLike} from "@/api/system/ordera";
import log from "@/views/monitor/job/log.vue";

export default {
  name: "Ordera",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      orderaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        onumber: null,
        price: null,
        orderStatus: null,
        itemStatus: null,
        itemCount: null,
        name: null,
        userId: null,
        groupId:null,
        communityId:null,
        oid:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.oid=this.$route.query.order;
    this.queryParams.communityId=this.$route.params.CommunityId;
    console.log(this.queryParams.communityId+"communityId")
    this.queryParams.groupId=this.$route.params.id;
    console.log(this.queryParams.groupId)
    this.queryParams.userId=this.$route.params.userId;
    if(this.queryParams.userId!=null || this.queryParams.groupId!=null || this.queryParams.communityId!=null){
      this.queryParams.pageNum=1;
      this.queryParams.pageSize=1000;
    }

// 初始化 itemStatus 为第一个可能的值
    this.queryParams.itemStatus = this.$route.params.itemStatus0;

// 如果 itemStatus0 无效，则尝试从 itemStatus1 获取值
    if (this.queryParams.itemStatus == null || this.queryParams.itemStatus === "") {
      this.queryParams.itemStatus = this.$route.params.itemStatus1;
    }

// 如果 itemStatus 仍然无效，则尝试从 orderNum0 获取值
    if (this.queryParams.itemStatus == null || this.queryParams.itemStatus === "") {
      this.queryParams.itemStatus = this.$route.params.orderNum0;
    }

// 如果 itemStatus 仍然无效，则尝试从 orderNum1 获取值
    if (this.queryParams.itemStatus == null || this.queryParams.itemStatus === "") {
      this.queryParams.itemStatus = this.$route.params.orderNum1;
    }

// 如果 itemStatus 仍然无效，则尝试从 ItemStatus0 获取值
    if (this.queryParams.itemStatus == null || this.queryParams.itemStatus === "") {
      this.queryParams.itemStatus = this.$route.params.ItemStatus0;
    }

// 如果 itemStatus 仍然无效，则尝试从 ItemStatus1 获取值
    if (this.queryParams.itemStatus == null || this.queryParams.itemStatus === "") {
      this.queryParams.itemStatus = this.$route.params.ItemStatus1;
    }

    console.log(this.queryParams.groupId); // 如果需要的话打印 groupId
    console.log(this.queryParams.itemStatus+"itemStatus")
    console.log(this.queryParams.itemStatus+"最后的itemStatus")
    console.log(this.queryParams.userId)
    this.getList();
  },
  methods: {
    /** 查询订单管理列表 */
    getList() {

      if(this.queryParams.communityId==null && this.queryParams.oid==null){
        this.loading = true;
        listOrdera(this.queryParams).then(response => {
          this.orderaList = response.data.list;
          console.log(this.orderaList+"::  t")
          this.total = response.data.total;
          this.loading = false;
        });
      }
      if(this.queryParams.oid!=null){
        this.loading = true;
        orderLike(this.queryParams).then(res=>{
          this.orderaList = res.list;
          this.total = res.total;
          this.loading = false;
        })
      }
      if(this.queryParams.communityId!=null) {
        this.loading = true;
        itemOrder0(this.queryParams).then(response =>{
          this.orderaList = response.data.list;
          console.log(this.orderaList+"::  t")
          this.total = response.data.total;
          this.loading = false;
        })
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    handleSizeChangea(val) {
      this.queryParams.pageSize = val;
      this.getList(); // 假设这是你的数据获取函数
    },
    handleCurrentChangea(val) {
      this.queryParams.pageNum = val;
      this.getList(); // 同样地，假设这是你的数据获取函数
    },
    // 点击订单名称跳转到拼团管理
    GroupNameRouter(row){
      this.$router.push({
        name: 'GroupManage',
        params: {
          groupName: row.name,
        }
      })
    },
    // 点击名称跳转到user根据名称查询
    UserB(row){
      this.$router.push({
        name: 'Userb',
        params: {
          userId:row.userId,
          // nickName: row.userName,
        }
      })
    },
    // 点击商铺根据商铺姓名进行查询
    store(row){
      this.$router.push({
        name: 'StoreMange',
        params: {
          storeName: row.storeName1,
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        oid: null,
        onumber: null,
        price: null,
        orderStatus: null,
        itemStatus: null,
        createTime: null,
        itemCount: null,
        userName1: null,
        phone1:null,
        storeName1:null,
        name1:null,
        reservedPhone:null,
        reservedName:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      // this.handleQuery();
      this.$route.params.userId=null;
      this.$route.params.orderNum0=null;
      this.$route.params.orderNum1=null;
      this.queryParams.userId=null;
      this.queryParams.itemStatus=null;
      // 刷新当前页面
      window.location.reload();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.oid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const oid = row.oid || this.ids
      getOrdera(oid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.oid != null) {
            updateOrdera(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrdera(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const oids = row.oid || this.ids;
      this.$modal.confirm('是否确认删除订单管理编号为"' + oids + '"的数据项？').then(function() {
        return delOrdera(oids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/ordera/export', {
        ...this.queryParams
      }, `ordera_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
